package com.zenithmind.user.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.result.Result;
import com.zenithmind.user.pojo.vo.DashboardStatsVO;
import com.zenithmind.user.pojo.vo.UserGrowthVO;
import com.zenithmind.user.pojo.vo.SystemMetricsVO;
import com.zenithmind.user.pojo.vo.TodayOverviewVO;
import com.zenithmind.user.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 仪表盘控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/v1/dashboard")
@Tag(name = "仪表盘管理", description = "仪表盘数据统计接口")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    @GetMapping("/stats")
    @Operation(summary = "获取仪表盘统计数据", description = "获取用户、课程、考试、资讯等统计数据")
    @RequireAuth(admin = true)
    @Log(description = "获取仪表盘统计数据", operationType = OperationTypeEnum.QUERY)
    public Result<DashboardStatsVO> getDashboardStats() {
        log.info("获取仪表盘统计数据");
        DashboardStatsVO stats = dashboardService.getDashboardStats();
        return Result.success(stats);
    }

    @GetMapping("/user-growth")
    @Operation(summary = "获取用户增长趋势", description = "获取指定时间范围内的用户增长趋势数据")
    @RequireAuth(admin = true)
    @Log(description = "获取用户增长趋势", operationType = OperationTypeEnum.QUERY)
    public Result<List<UserGrowthVO>> getUserGrowthTrend(
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate,
            @Parameter(description = "时间粒度") @RequestParam(defaultValue = "day") String granularity) {
        log.info("获取用户增长趋势: {} - {}, 粒度: {}", startDate, endDate, granularity);
        List<UserGrowthVO> growthData = dashboardService.getUserGrowthTrend(startDate, endDate, granularity);
        return Result.success(growthData);
    }

    @GetMapping("/access-stats")
    @Operation(summary = "获取系统访问统计", description = "获取系统访问量、页面浏览量等统计数据")
    @RequireAuth(admin = true)
    @Log(description = "获取系统访问统计", operationType = OperationTypeEnum.QUERY)
    public Result<Map<String, Object>> getSystemAccessStats(
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate) {
        log.info("获取系统访问统计: {} - {}", startDate, endDate);
        Map<String, Object> accessStats = dashboardService.getSystemAccessStats(startDate, endDate);
        return Result.success(accessStats);
    }

    @GetMapping("/recent-activities")
    @Operation(summary = "获取最近活动", description = "获取系统最近的用户活动记录")
    @RequireAuth(admin = true)
    @Log(description = "获取最近活动", operationType = OperationTypeEnum.QUERY)
    public Result<List<Map<String, Object>>> getRecentActivities(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        log.info("获取最近活动, 限制数量: {}", limit);
        List<Map<String, Object>> activities = dashboardService.getRecentActivities(limit);
        return Result.success(activities);
    }

    @GetMapping("/popular-content")
    @Operation(summary = "获取热门内容", description = "获取热门文章、课程、考试等内容统计")
    @RequireAuth(admin = true)
    @Log(description = "获取热门内容", operationType = OperationTypeEnum.QUERY)
    public Result<Map<String, Object>> getPopularContent() {
        log.info("获取热门内容统计");
        Map<String, Object> popularContent = dashboardService.getPopularContent();
        return Result.success(popularContent);
    }

    @GetMapping("/system-metrics")
    @Operation(summary = "获取系统性能指标", description = "获取系统CPU、内存、存储等性能指标")
    @RequireAuth(admin = true)
    @Log(description = "获取系统性能指标", operationType = OperationTypeEnum.QUERY)
    public Result<SystemMetricsVO> getSystemMetrics() {
        log.info("获取系统性能指标");
        SystemMetricsVO metrics = dashboardService.getSystemMetrics();
        return Result.success(metrics);
    }

    @GetMapping("/online-users")
    @Operation(summary = "获取实时在线用户数", description = "获取当前在线用户数量")
    @RequireAuth(admin = true)
    @Log(description = "获取实时在线用户数", operationType = OperationTypeEnum.QUERY)
    public Result<Integer> getOnlineUsers() {
        log.info("获取实时在线用户数");
        Integer onlineCount = dashboardService.getOnlineUsers();
        return Result.success(onlineCount);
    }

    @GetMapping("/today-overview")
    @Operation(summary = "获取今日数据概览", description = "获取今日新增用户、访问量等概览数据")
    @RequireAuth(admin = true)
    @Log(description = "获取今日数据概览", operationType = OperationTypeEnum.QUERY)
    public Result<TodayOverviewVO> getTodayOverview() {
        log.info("获取今日数据概览");
        TodayOverviewVO overview = dashboardService.getTodayOverview();
        return Result.success(overview);
    }
}
